package com.derbysoft.ota.application.translator.rq;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.application.util.UniqueIDTypes;
import com.derbysoft.ota.interfaces.protocol._2009a.OTACancelRQ;
import com.derbysoft.ota.interfaces.protocol._2009a.TransactionActionType;
import com.derbysoft.ota.interfaces.protocol._2009a.OTACancelRQ.UniqueID;
import com.derbysoft.remote.dswitch.generic.constants.UniqueIDType;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelReservationIDDTO;
import com.google.common.base.Preconditions;
import com.googlecode.functionalcollections.FunctionalIterables;

public class CancelRQTranslator implements Translator<OTACancelRQ, CancelRQ> {

	@Override
	public CancelRQ translate(OTACancelRQ source) {
		TransactionActionType cancelType = Preconditions.checkNotNull(source.getCancelType(), "CancelType required.");
		Preconditions.checkArgument(
			TransactionActionType.COMMIT == cancelType, 
			"Sorry, we only support CancelType of [" + TransactionActionType.COMMIT.value() + "]"
		);
		
		CancelRQ dest = new CancelRQ();
		dest.getHotelReservationIDs().add(of(ersp(source)));
		return dest;
	}

	private UniqueID ersp(OTACancelRQ source) {
		return Preconditions.checkNotNull(
			FunctionalIterables.make(source.getUniqueID()).detect(UniqueIDTypes.ersp()), 
			"Please provide us the UniqueID of type 22(UIT -> ERSP)."
		);
	}

	private HotelReservationIDDTO of(UniqueID ersp) {
		return new HotelReservationIDDTO(UniqueIDType.ERSP, ersp.getID());
	}


}
